package com.gary.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gary.demo.biz.CustomerBO;
import com.gary.demo.entity.Customer;
import com.gary.demo.vo.CustomerVO;
import com.gary.demo.vo.param.CustomerOperateParamVO;
import com.gary.demo.vo.param.CustomerQueryParamVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerBO customerBO;

    @PostMapping("/add")
    public void add(@RequestBody CustomerOperateParamVO paramVO) {
        customerBO.add(paramVO);
    }

    @PostMapping("/update")
    public void update(@RequestBody CustomerOperateParamVO paramVO) {
        customerBO.update(paramVO);
    }

    @GetMapping("/list")
    public List<CustomerVO> list(@RequestParam(required = false) String name) {
        List<Customer> list = customerBO.list(name);
        return list.stream().map(item -> {
            CustomerVO customerVO = new CustomerVO();
            BeanUtils.copyProperties(item, customerVO);
            return customerVO;
        }).collect(Collectors.toList());
    }

    @GetMapping("pageQuery")
    public IPage<CustomerVO> pageQuery(CustomerQueryParamVO paramVO) {
        IPage<Customer> customerIPage = customerBO.pageQuery(paramVO);
        return null;
    }
}
